package cn.snow.iot.module.warning.service.record;

import cn.snow.iot.framework.common.pojo.CommonResult;
import cn.snow.iot.module.cps.api.equipment.AdminEquipmentApi;

import cn.snow.iot.module.warning.controller.admin.home.vo.RecordVO;
import cn.snow.iot.module.warning.controller.admin.home.vo.WarningEquipmentVO;
import cn.snow.iot.module.warning.controller.admin.record.vo.WarningRecordPageVO;
import cn.snow.iot.module.warning.controller.admin.record.vo.WarningRecordStatusVO;
import cn.snow.iot.module.warning.dal.dataobject.config.WarningDO;
import cn.snow.iot.module.warning.dal.dataobject.record.WarningRecordDO;
import cn.snow.iot.module.warning.dal.mysql.record.WarningRecordMapper;
import cn.snow.iot.module.warning.enums.WarningRecord;
import cn.snow.iot.module.warning.service.config.IWarningService;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class WarningRecordServiceImpl extends ServiceImpl<WarningRecordMapper, WarningRecordDO> implements IWarningRecordService {

    @Resource
    private IWarningService warningService;

    @Resource
    private AdminEquipmentApi equipmentApi;

    @Override
    public IPage<WarningRecordDO> selectRecordPage(WarningRecordPageVO recordPageVO) {
        IPage<WarningRecordDO> page = new Page<>(recordPageVO.getPageNo(), recordPageVO.getPageSize());
        return baseMapper.selectWarningPage(page, recordPageVO);
    }

    @Override
    public CommonResult<WarningRecordDO> updateStatus(WarningRecordStatusVO recordStatusVO) {
        if (recordStatusVO.getId() != null) {
            WarningRecordDO warningRecordDO = new WarningRecordDO();
            warningRecordDO.setId(recordStatusVO.getId());
            if (recordStatusVO.getStatus() != null) {
                warningRecordDO.setStatus(recordStatusVO.getStatus());
                warningRecordDO.setHandleUser(recordStatusVO.getHandleUser());
                LocalDateTime now = LocalDateTime.now();
                warningRecordDO.setHandleTime(now);
                warningRecordDO.setRemark(recordStatusVO.getRemark());
                updateById(warningRecordDO);
                return CommonResult.success(warningRecordDO);
            } else {
                return CommonResult.error(400, "参数错误");
            }
        } else {
            return CommonResult.error(400, "参数错误");
        }
    }

    /**
     * 告警事件持久化mysql监听器调用方法
     * @param jsonObject
     * @return
     */
    public void handleWarningRecord(JSONObject jsonObject) {
        WarningRecordDO warningRecordDO = new WarningRecordDO();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

        Long configId = jsonObject.getLong("warningId");
        String warningTime = jsonObject.getString("thetime");
        String uuId = jsonObject.getString("uuId");
        WarningDO warningDO = warningService.getByRuleId(configId);

        LocalDateTime dateTime = LocalDateTime.parse(warningTime, formatter);
        String pointCode = warningDO.getPointCode();
        Object value = jsonObject.get(pointCode);
        String pointValue = String.valueOf(value);

        //设置uuId,用于关联此次告警记录对应的动作执行记录
        warningRecordDO.setWarningTag(uuId);
        warningRecordDO.setPointValue(pointValue);
        warningRecordDO.setConfigId(warningDO.getId());
        warningRecordDO.setConfigName(warningDO.getWarningName());
        warningRecordDO.setWarningData(String.valueOf(jsonObject));
        // 设备信息
        warningRecordDO.setEquipmentId(warningDO.getEquipmentId());
        warningRecordDO.setEquipmentName(warningDO.getEquipmentName());
        // 点位信息
        warningRecordDO.setPointId(warningDO.getPointId());
        warningRecordDO.setPointName(warningDO.getPointName());
        warningRecordDO.setWarningTime(dateTime);
        warningRecordDO.setStatus(WarningRecord.DISABLE);

        save(warningRecordDO);
    }


    @Override
    public CommonResult<WarningRecordDO> insertRecord(JSONObject jsonObject) {
        WarningRecordDO warningRecordDO = new WarningRecordDO();
//        JSONObject jsonObject =  JSON.parseObject(data);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

        Long configId = (Long) jsonObject.get("warningId");
        String warningTime = (String) jsonObject.get("thetime");
        WarningDO warningDO = warningService.getByRuleId(configId);

        LocalDateTime dateTime = LocalDateTime.parse(warningTime, formatter);
        String pointCode = warningDO.getPointCode();
        Object value = jsonObject.get(pointCode);
        String pointValue = String.valueOf(value);


        warningRecordDO.setPointValue(pointValue);
        warningRecordDO.setConfigId(warningDO.getId());
        warningRecordDO.setConfigName(warningDO.getWarningName());
        warningRecordDO.setWarningData(String.valueOf(jsonObject));
        // 设备信息
        warningRecordDO.setEquipmentId(warningDO.getEquipmentId());
        warningRecordDO.setEquipmentName(warningDO.getEquipmentName());
        // 点位信息
        warningRecordDO.setPointId(warningDO.getPointId());
        warningRecordDO.setPointName(warningDO.getPointName());
        warningRecordDO.setWarningTime(dateTime);
        warningRecordDO.setStatus(WarningRecord.DISABLE);

        save(warningRecordDO);
        return CommonResult.success(warningRecordDO);
    }

    @Override
    public List<RecordVO> getRecordList() {
        return baseMapper.getRecordList();
    }

    @Override
    public List<WarningEquipmentVO> getWarningEquipmentList() {
        return baseMapper.getWarningEquipmentList();
    }

}
